home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtoys04.zip / DPMI.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-07  |  4KB  |  147 lines

  1. (***************************************************************************
  2.   DPMI unit
  3.   DPMI simplification routines
  4.   PJB October 6, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright PJB 1993, All Rights Reserved.
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9.   This unit will not compile if DPMI is not defined, must set
  10.   Compile│Target to Protected.
  11.  
  12. ***************************************************************************)
  13. unit DPMI;
  14. {$R-,S-,X+}
  15.  
  16. interface
  17.  
  18.   type
  19.     TRealRegs =
  20.       record
  21.         RealEDI:   Longint;
  22.         RealESI:   Longint;
  23.         RealEBP:   Longint;
  24.         Reserved:  Longint;
  25.         RealEBX:   Longint;
  26.         RealEDX:   Longint;
  27.         RealECX:   Longint;
  28.         RealEAX:   Longint;
  29.         RealFlags: Word;
  30.         RealES:    Word;
  31.         RealDS:    Word;
  32.         RealFS:    Word;
  33.         RealGS:    Word;
  34.         RealIP:    Word;
  35.         RealCS:    Word;
  36.         RealSP:    Word;
  37.         RealSS:    Word;
  38.       end;
  39.  
  40.     SelectorRec =
  41.       record
  42.         Selector : Word;
  43.         Segment  : Word;
  44.       end;
  45.  
  46.   var
  47.     RealRegs : TRealRegs ABSOLUTE System.RealModeRegs;
  48.  
  49.   function  RealToLinear(RealAdr:Pointer):Longint;
  50.   function  CreateRealModeSelector(RealAdr:Pointer; Size:Word):Pointer;
  51.   function  GetDosMem(var RealModePtr, ProtectedModePtr:Pointer; Size:Word):boolean;
  52.   procedure RealModeInterrupt(Int:Word);
  53.  
  54. (***************************************************************************
  55. ***************************************************************************)
  56. implementation
  57.  
  58.     uses
  59.       WinAPI;
  60.  
  61.  
  62.   (*******************************************************************
  63.     Calculate real address' linear address
  64.   *******************************************************************)
  65.   function RealToLinear; assembler;
  66.   asm
  67.     mov  ax,RealAdr.Word+2
  68.  
  69.     mov  cl,4
  70.     rol  ax,cl
  71.  
  72.     mov  dx,ax
  73.     and  dx,0000Fh
  74.     and  ax,0FFF0h
  75.  
  76.     add  ax,RealAdr.Word
  77.     adc  dx,0
  78.   end;
  79.  
  80.  
  81.   (*******************************************************************
  82.     Create a real mode memory area selector
  83.   *******************************************************************)
  84.   function CreateRealModeSelector;
  85.     var
  86.       NewSel : word;
  87.   begin
  88.     NewSel:=AllocSelector(0);
  89.     SetSelectorBase(NewSel, RealToLinear(RealAdr));
  90.     SetSelectorLimit(NewSel, Size);
  91.     CreateRealModeSelector:=Ptr(NewSel, 0);
  92.   end;
  93.  
  94.  
  95.   (*******************************************************************
  96.     Allocate real mode memory
  97.   *******************************************************************)
  98.   function GetDosMem;
  99.     var
  100.       DosBlock      : SelectorRec;
  101.   begin
  102.     LongInt(DosBlock):=GlobalDosAlloc(Size);
  103.     ProtectedModePtr:=Ptr(DosBlock.Selector, 0);
  104.     RealModePtr:=Ptr(DosBlock.Segment, 0);
  105.     GetDosMem:=LongInt(DosBlock)<>0;
  106.   end;
  107.  
  108.  
  109.   (*******************************************************************
  110.     Call a real mode interrupt
  111.   *******************************************************************)
  112.   procedure RealModeInterrupt; assembler;
  113.   asm
  114.       mov  RealRegs.RealEDI.Word,di
  115.       mov  di,OFFSET RealModeRegs
  116.  
  117.       mov  word ptr [di].TRealRegs.RealSP,0
  118.       mov  word ptr [di].TRealRegs.RealSS,0
  119.  
  120.       mov  word ptr [di].TRealRegs.RealEAX.Word,ax
  121.       mov  word ptr [di].TRealRegs.RealEBX.Word,bx
  122.       mov  word ptr [di].TRealRegs.RealECX.Word,cx
  123.       mov  word ptr [di].TRealRegs.RealEDX.Word,dx
  124.  
  125.       push ds
  126.       pop  es
  127.       mov  ax,0300h
  128.       mov  bx,Int
  129.       xor  cx,cx
  130.       int  31h
  131.  
  132.       mov  di,OFFSET RealModeRegs
  133.  
  134.       mov  ax,word ptr [di].TRealRegs.RealEAX.Word
  135.       mov  bx,word ptr [di].TRealRegs.RealEBX.Word
  136.       mov  cx,word ptr [di].TRealRegs.RealECX.Word
  137.       mov  dx,word ptr [di].TRealRegs.RealEDX.Word
  138.  
  139.       mov  di,word ptr [di].TRealRegs.RealEDI.Word
  140.   end;
  141.  
  142.  
  143.     (*******************************************************************
  144.     *******************************************************************)
  145.  
  146. end.
  147.